{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b2b6eb8a",
   "metadata": {},
   "source": [
    "* 2022/10/07\n",
    "* python课堂记录：许丹"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "671b2c9a",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "* 1.有序的数据结构---字符串（str）\n",
    "* 2.有序的数据结构---列表（list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a28a5f2f",
   "metadata": {},
   "source": [
    " 1.1 [关于字符串的基本内容](https://docs.python.org/3/tutorial/introduction.html#strings)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b9d047e0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'span eggs'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'span eggs'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "164fa899",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "print(type('span eggs'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "117e195d",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (3578565646.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [4]\u001b[1;36m\u001b[0m\n\u001b[1;33m    'doesn't'\u001b[0m\n\u001b[1;37m           ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "'doesn't'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "65edbd8f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"doesn't\""
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 以上解决方案\n",
    "\"doesn't\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "5fc5e610",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"doesn't\""
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'doesn\\'t'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "55ab3682",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"doesn't\""
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''doesn't'''  #三引号：保留字符串信息的格式格式,保留完整的数据信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "97e0cbcd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'<script type=\"application/ld+json\">\\n     {\\n       \"@context\": \"https://schema.org\",\\n       \"@type\": \"WebSite\",\\n       \"url\": \"https://www.python.org/\",\\n       \"potentialAction\": {\\n         \"@type\": \"SearchAction\",\\n         \"target\": \"https://www.python.org/search/?q={search_term_string}\",\\n         \"query-input\": \"required name=search_term_string\"\\n       }\\n     }\\n    </script>'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''<script type=\"application/ld+json\">\n",
    "     {\n",
    "       \"@context\": \"https://schema.org\",\n",
    "       \"@type\": \"WebSite\",\n",
    "       \"url\": \"https://www.python.org/\",\n",
    "       \"potentialAction\": {\n",
    "         \"@type\": \"SearchAction\",\n",
    "         \"target\": \"https://www.python.org/search/?q={search_term_string}\",\n",
    "         \"query-input\": \"required name=search_term_string\"\n",
    "       }\n",
    "     }\n",
    "    </script>'''"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36d0717f",
   "metadata": {},
   "source": [
    "###  1.2 [关于字符串的方法及函数](https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str)\n",
    "\n",
    ">1.字符串是可以拼接的（字符串的运算）  \n",
    ">2.字符串是由顺序的,从第一个字符开始，从0依次往后，有长度,可以用len（）检查字符串的长度  \n",
    ">3.字符串的取值  \n",
    ">>1.取单个值:从左往右（正数，第一个值是0），从右往左（负数，第一个值是-1）  \n",
    ">> 2.取多个值：切片的方法，两个值作为取值的区间  \n",
    ">4.字符串的方法及函数  \n",
    ">> 1.in 查询成员是否存在   \n",
    ">> 2.str.count()查询数量 \n",
    ">> 3.str.isdigit()判断字符串是否是数字  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "4c8c548e",
   "metadata": {},
   "outputs": [],
   "source": [
    "new_name = [\"蔡何欣妍\",\"小红\",\"张杰\",\"许丹\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "43bb6005",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "广州南方学院团委欢迎蔡何欣妍\n",
      "广州南方学院团委欢迎小红\n",
      "广州南方学院团委欢迎张杰\n",
      "广州南方学院团委欢迎许丹\n"
     ]
    }
   ],
   "source": [
    "for i in new_name:\n",
    "    print(\"广州南方学院团委欢迎\"+i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "d8c67538",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'广州南方学院团委欢迎蔡何欣妍'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"广州南方学院团委欢迎\"+\"蔡何欣妍\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a4c75003",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'广州南方学院团委欢迎蔡何欣妍'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text = '广州南方学院团委欢迎蔡何欣妍'\n",
    "text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "47793400",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'广'"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "a118f598",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'州'"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "8df72244",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'南'"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "d96209e1",
   "metadata": {},
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "string index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "Input \u001b[1;32mIn [31]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mtext\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m15\u001b[39;49m\u001b[43m]\u001b[49m\n",
      "\u001b[1;31mIndexError\u001b[0m: string index out of range"
     ]
    }
   ],
   "source": [
    "text[15]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "f900b977",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "14"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "03fbbcb2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'团委'"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[6]+text[7]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "eeb8afb6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'团委'"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用切片的方法进行多个值取值\n",
    "text[6:8]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd9bdd56",
   "metadata": {},
   "source": [
    "* in 查询成员"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "40593810",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'广州' in text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "44c9e19d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'北京' in text"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e467cc62",
   "metadata": {},
   "source": [
    "* str.count()查询数量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "118e1050",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text.count('广州')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a5d012cd",
   "metadata": {},
   "source": [
    "* str.isdigit()判断字符串是否是数字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "2bd6f8a9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "19  <class 'str'>\n",
      "19 <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "age_li = '19 '\n",
    "age_wang = '19'\n",
    "print(age_li,type(age_li))\n",
    "print(age_wang,type(age_wang))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "278fe626",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_wang.isdigit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "74ba98aa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text.isdigit()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7230a6d",
   "metadata": {},
   "source": [
    "## 2.有序的数据结构--列表(list)\n",
    "\n",
    ">1.列表的基本运算法则（参考：有序的数据结构的运算法则列表）  \n",
    ">2.列表的一般方法（要求：全部掌握，并且做总结在课后笔记中）  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1dac10fa",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 思考：假设，有这样一个场景，我要录入/存放一个班的同学的信息（名单信息），用字符串存放是否可行？如何取值呢？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "4856b533",
   "metadata": {},
   "outputs": [],
   "source": [
    "网二_name = \"丽华，小明，小红，张强\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "a3ab5beb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'丽华'"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字符串取值,比较麻烦\n",
    "网二_name[0:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "7c725aed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'小明'"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_name[3:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "b94211af",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'小红'"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_name[6:8]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b37c948d",
   "metadata": {},
   "source": [
    "* 对比字符串和列表  \n",
    ">1. 字符串是按照字符进行数据取值  \n",
    ">2.列表是按照元素进行数据取值  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "7c463307",
   "metadata": {},
   "outputs": [],
   "source": [
    "网二_name_list = [\"丽华\",\"小明\",\"小红\",\"李强\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "9c7e695a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(网二_name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "2fa52b4b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'丽华'"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_name_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "d8b5d1b9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小红', '李强']"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_name_list[-2:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "fa8064c5",
   "metadata": {},
   "outputs": [],
   "source": [
    "网二_age_list = [19,18,19,20]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "658c0343",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "19"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_age_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "9a38b715",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[19, 18, 19, 20]"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_age_list[:]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dbcb1668",
   "metadata": {},
   "source": [
    "### 2.2 列表的方法\n",
    "\n",
    ">1.增加  \n",
    ">2.删除  \n",
    ">3.修改  \n",
    ">4.查询in，count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "b81812cd",
   "metadata": {},
   "outputs": [],
   "source": [
    "names = [\"Mike\",\"Marry\",\"Jan\",\"Jack\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9db9d0ab",
   "metadata": {},
   "source": [
    "* 学习方法\n",
    ">1.listname.Tab键 查看所有方法名称  \n",
    ">2.help(listname.function)查看其中一个方法的具体操作内容  \n",
    "\n",
    "> 课后练习（做一个表格，总结列表的方法）：\n",
    "\n",
    "\n",
    "| 列表方法 | Description | example | 备注 |\n",
    "| --------| -------- | -------- | -------- |\n",
    "| append()   | 增加元素，增加在列表的末尾 |  names.append(\"zhichao\")， ['Mike', 'Marry', 'Jan', 'Jack', 'zhichao']\t|  |\n",
    "| Paragraph | Text |  |  |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "2fba0705",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function append:\n",
      "\n",
      "append(object, /) method of builtins.list instance\n",
      "    Append object to the end of the list.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.append)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "6b80dcf1",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.append(\"zhichao\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "2adc4f3d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Mike', 'Marry', 'Jan', 'Jack', 'zhichao']"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "85fca04d",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function insert:\n",
      "\n",
      "insert(index, object, /) method of builtins.list instance\n",
      "    Insert object before index.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.insert)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "05a712c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.insert(1,\"Alex\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dbb2a165",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "be76a308",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Mike', 'Alex', 'Marry', 'Jan', 'Jack', 'zhichao']"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e11adeb1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
